package com.peanut.baby.mvp.mycontent;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.LiveRoom;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/10.
 */

public class CoursePresenter implements CourseContract.Presenter{
    CourseContract.View view;
    Activity activity;
    public CoursePresenter(CourseContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getCourseList(String userId,int pageNo, int pageSize) {
        String token = InitManager.getInstance().getUser()==null?"":InitManager.getInstance().getUser().userToken;
        Observable observable = RetrofitClient.getInstance().getSelfLiveRoomList(userId,pageNo,pageSize,token);
        observable.compose(BaseScheduler.<BaseResponse<List<LiveRoom>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<LiveRoom>>() {
            @Override
            protected void onHandleSuccess(List<LiveRoom> qas) {
                view.onGetCourseListSuccess(qas);
            }

            @Override
            protected void onHandleError(String code,String error) {
                view.onRequestFailed(error);
            }
        });
    }

    @Override
    public void joinRoom(String userId,LiveRoom roomId) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                userId,roomId.roomId+"");
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onHandleSuccess(String o) {
                        view.onJoinSuccess(roomId);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onJoinFailed(code,msg);
                    }

                });
    }
}
